package websocket

import (
	"log"
	"net/http"
	"os"
)

//func HandleCon(w http.ResponseWriter, r *http.Request) {
//
//}

//https://github.com/gorilla/websocket/blob/master/examples/chat/main.go

func serveHome(w http.ResponseWriter, r *http.Request) {
	log.Println(r.URL)
	if r.URL.Path != "/" {
		http.Error(w, "Not found", http.StatusNotFound)
		return
	}
	if r.Method != http.MethodGet {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	log.Println(os.Args)
	//go build 之后，放到项目 根目录下 {root}/internal/websocket/chat.html
	http.ServeFile(w, r, "./internal/websocket/chat.html")
}

func UseWebsocketCase() {
	hub := newHub()
	go hub.run()
	//go hub.run()
	//go hub.run()
	http.HandleFunc("/", serveHome)
	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
		serveWs(hub, w, r)
	})
	var port = ":8080"
	log.Println("server ... ", port)
	err := http.ListenAndServe(port, nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
